<?php

namespace app\common\controller;
use app\common\service\TranslateService;

class Common extends \think\Controller {

	public $lang;
	public $ip;
	
	public function initialize() {
		parent::initialize();
		//跨域处理
		header('Access-Control-Allow-Origin:*');

		$this->lang = P('lang')?:'hi';
		if ($this->lang == 'zh_CN') {
			$this->lang = 'zh';
		}
		// if ($this->lang == 'yn') {
		// 	$this->lang = 'id';
		// }
		$url   = request()->baseUrl();
		$param = request()->param();
		$uid   = isset($param['uid'])?$param['uid']:'';
		$data  = '';
		unset($param[$url]);
		unset($param['action']);
		unset($param['func']);
		unset($param['api']);
		foreach ($param as $key => $val) {
			$data .= $key.'='.$val.' & ';
		}
		$ip = request()->ip();
		include_once('iplimit.class.php');
		$iplimit = new iplimit;
		if($iplimit->setup($ip))
        {
        	// die('ip limit');
        }
		$this->ip = $ip;
		model('IpModel')->addData($uid,$ip,$url,$data);
	}

	/**
	 * json返回
	 * @param  [type] $code 状态码
	 * @param  [type] $msg  消息
	 * @param  [type] $data 返回数据
	 * @return [type]       [description]
	 */
	protected function retData($code = 0, $msg = 'success', $data = []) {
		$msg = TranslateService::baiduApi($msg,$this->lang);
		if($code == 1 || $code == -1){
			$data = $msg;
		} elseif ($code == 0) {
			if(empty($data) && $data != 0){
				$data = [];
			}
		}
		echo json_encode([
			'code'=>$code,
			'msg' =>$msg,
			'data'=>$data
		],320);
		die();
		exit();
	}


	public function getYesDate(){
		return date('Y-m-d',strtotime('-1 days'));
	}

	public function getWeekDate(){
		return date('Y-m-d', time()-86400*date('w')+(date('w')>0?86400:-6*86400));
	}

	public function getMoothDate(){
		return date('Y-m-d', mktime(0,0,0,date('m'),1,date('Y')));
	}
}
